﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmHopAdd : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();

        public frmHopAdd()
        {
            InitializeComponent();
        }

        private void frmHopAdd_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            searchLookUpEdit_NhanVien.Text = "";
            memoEdit_LyDo.Text = "";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                Validation();
                string userid = searchLookUpEdit_NhanVien.EditValue.ToString();
                DateTime ngayhop = Convert.ToDateTime(dateEdit_NgayHop.EditValue);
                TimeSpan giohop = timeEdit_GioHop.Time.TimeOfDay;
                string lydo = memoEdit_LyDo.Text;
                string trangthai = "Not finish";
                AddHop(userid, ngayhop, giohop, lydo, trangthai);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region "Coding"
        private void LoadData()
        {
            //Load user to search look up edit
            LoadUser();
            searchLookUpEdit_NhanVien.Properties.DataSource = ds.Tables["TableUser"];
            searchLookUpEdit_NhanVien.Properties.ValueMember = "UserID";
            searchLookUpEdit_NhanVien.Properties.DisplayMember = "FullName";
            //Load ngay bat dau mac dinh la ngay hom nay
            dateEdit_NgayHop.EditValue = DateTime.Now;
        }

        private void LoadUser()
        {
            try
            {
                if (ds.Tables["TableUser"] != null)
                    ds.Tables["TableUser"].Clear();
                string sql = @"usp_AllUser";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableUser");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddHop(string userid, DateTime ngayhop, TimeSpan giohop, string lydo, string trangthai)
        {
            try
            {
                string sql = @"usp_InsertHop @userid = @uid, @ngayhop = @ngayhop, @giohop = @giohop, @lydo = @lydo, @trangthai = @trangthai";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@uid", userid);
                cmd.Parameters.AddWithValue("@ngayhop", ngayhop);
                cmd.Parameters.AddWithValue("@giohop", giohop);
                cmd.Parameters.AddWithValue("@lydo", lydo);
                cmd.Parameters.AddWithValue("@trangthai", trangthai);
                int i = 0;
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Thêm họp thành công");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Validation()
        {
            if (searchLookUpEdit_NhanVien.EditValue == null)
            {
                MessageBox.Show("Bạn chưa chọn nhân viên!");
                return;
            }
            if (dateEdit_NgayHop.EditValue == null)
            {
                MessageBox.Show("Bạn chưa chọn ngày họp");
                return;
            }
            if (timeEdit_GioHop.EditValue == null)
            {
                MessageBox.Show("Bạn chưa chọn giờ họp");
                return;
            }
            if (memoEdit_LyDo.Text == "")
            {
                MessageBox.Show("Bạn chưa ghi lý do");
                return;
            }
        }
        #endregion
    }
}
